﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using gregn6Lib;
using System.Configuration;
using Common;
using System.IO;
using NursingFormSystem.Model;
using DevExpress.XtraEditors;
using SQLDLL;
using DLL;
using BLL;
using NursingFormSystem.Common;
using System.Xml;

namespace NursingFormSystem
{
    public partial class NursingPrints : XtraForm
    {
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        private string[] PatientInfo;
        string[] user;
        string[] id;
        Tb_User LoginUser;
        ElectronicSignature es;
        public Tb_User InitLoginUser(string[] UserParam)
        {
            LoginUser = new Tb_User();
            LoginUser.UserId = UserParam[0];
            LoginUser.UserName = UserParam[1];
            LoginUser.DepartmentCode = UserParam[2];
            LoginUser.DepartmentName = UserParam[3];
            LoginUser.JobTitle = UserParam[4];
            LoginUser.PositionTitle = UserParam[5];
            return LoginUser;
        }
        public NursingPrints(string[] Id,string[] UserParm, string[] patientInfo)
        {
            InitializeComponent();
            this.PatientInfo = patientInfo;
            user = UserParm;
            id = Id;
            InitLoginUser(this.user);
            es = new ElectronicSignature(this.LoginUser);
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\NursingGrf.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }

        private void ReportFetchRecord()
        {
            string siginbase64 = string.Empty;
            //以下逻辑有问题 待修改 
            //string sql = " select top 1 ElectricSignBase64 from NursingInfoSystem.dbo.P_Personnel where UserId='{0}' and Name='{1}' ";
            //sql = string.Format(sql, user[0], user[1]);
            //object obj = SqlHelper.ExecuteScalar(CommandType.Text, sql, null);
            //if (obj != DBNull.Value && obj != null && !string.IsNullOrEmpty(obj.ToString()))
            //{
            //    siginbase64 = obj.ToString();
            //}
            string select = @"SELECT  c.*,CASE WHEN Systolicpressure = '' THEN '' ELSE Systolicpressure + '/' + Diastolicpressure END AS xueya,Date+' '+Time as ppp, p.ElectricSignBase64  FROM [BNNursingDocsSystem_Develop].[dbo].[t_ClinicalNursingPrint] c(NOLOCK),NursingInfoSystem.dbo.P_Personnel p(NOLOCK)  where c.NursName=p.Name and p.status<>0 ";
            select += string.Format(" AND ( c.patientId = '{0}' AND c.visit_ID = {1}) AND ( ", PatientInfo[0], PatientInfo[1]);
            if (id.Length == 1)
            {
                select += "c.NursingRecordID ='" + id[0] + "'";
            }
            else
            {
                for (int i = 0; i < id.Length; i++)
                {
                    select += "c.NursingRecordID ='" + id[i] + "'or ";
                }
                select = select.Substring(0, select.Length - 3);
            }
            select += ") ORDER BY CONVERT(DATETIME, datename(yyyy,getdate()) + '-' + Date + ' ' + Time, 120) ASC";
            DataSet ds = SqlHelper.ExecuteDataset(select, CommandType.Text);
            if (ds == null) return;
            if (ds.Tables.Count <= 0) return;
            Utility.FillRecordToReport(Report, ds.Tables[0]);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.axGRPrintViewer1.Start();
        }

        private void axGRPrintViewer1_Enter(object sender, EventArgs e)
        {

        }
    }
}
